package com.sise.bl.service.impl;

import com.sise.bl.dao.ProductDao;
import com.sise.bl.entity.PageBean;
import com.sise.bl.entity.Product;
import com.sise.bl.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    @Qualifier("productDao")
    private ProductDao productDao;

    @Override
    public PageBean<Product> findPage(String tid, int page, int pageSize) {
        long count = productDao.selectCountByTid(Integer.valueOf(tid));  // 查询总条数

        List<Product> products = productDao.selectProductByPage(page, pageSize, Integer.valueOf(tid));  //查询当前页对应的商品
//        int fi = (page - 1) * pageSize;
//        List<Product> products1 = new ArrayList<>();
//        for (int i = fi; i <= (fi + pageSize); i++) {
//            if (products.size() == (long)i || (long)i >products.size()) {
//                break;
//            }
//            products1.add(products.get(i));
//        }
        return new PageBean<Product>(products, page, pageSize, count);
    }

    @Override
    public Product findProductByPid(String pid) {
        return productDao.selectProductByPid(Integer.valueOf(pid));
    }

    @Override
    public int saveProduct(Product product) {
        int i = productDao.addProduct(product);
        return i;
    }

    @Override
    public List<Product> queryAll() {
        List<Product> products = productDao.selectAll();
        return products;
    }

    @Override
    public List<Product> queryByTid(Integer tid) {
        List<Product> products = productDao.selectByTid(tid);
        return products;
    }
}
